const app = getApp();
Page({
    data: {
        email:'',
        code:'',
        pwd:'',
        pwdComfirm:'',
        codeBtnTip:'发送验证码',
        sendCodeBtnDisable:false,
        api:app.globalData.api
    },
    bindInputEmail(e){
        this.setData({
            email: e.detail.value,
        })
    },
    bindInputCode(e){
        this.setData({
            code: e.detail.value,
        })
    },
    bindInputPwd(e){
        this.setData({
            pwd: e.detail.value,
        })
    },
    bindInputPwdAgain(e){
        this.setData({
            pwdComfirm: e.detail.value,
        })
    },
    bindSendCode(){
        let rep = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
        if(!rep.test(this.data.email)){
            my.showToast({
                type: 'fail',
                content: '请输入正确的邮箱格式！',
                duration: 2000
            });
            return false;
        }
        this.countDown(60);
        //调获取验证码的接口
        my.request({
            url: this.data.api+'/sendCode',
            method: 'POST',
            data: {
                email: this.data.email,
                action:2
            },
            headers:{
                'content-type':'application/json'  //默认值
            },
            dataType: 'json',
            success: function(res) {
                console.log(res)
                if(res.data.errCode==9){
                    my.showToast({
                        type: 'fail',
                        content: res.data.message,
                        duration: 2000
                    });
                }else if(res.data.errCode==0){
                    my.showToast({
                        type: 'success',
                        content: res.data.message,
                        duration: 2000
                    });
                }

            }
        });

    },
    countDown(num){
        let countDownNum = num;
        this.setData({
            sendCodeBtnDisable: true
        })
        let timer=setInterval(() => {
            this.setData({ 
                codeBtnTip: countDownNum+"s"
            })
            countDownNum--;
            if(countDownNum<0){
                clearInterval(timer);
                this.setData({
                    sendCodeBtnDisable: false,
                    codeBtnTip:"发送验证码"
                })
            }
        },1000);
    },
    registerSubmit(){
        //判断不为空；判断邮箱；判断密码长度；判断密码是否一致
        if( !this.data.email || !this.data.code || !this.data.pwd || !this.data.pwdComfirm ){
           my.alert({title: '漏填啦'});
           return false
        }
        let rep = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
        if(!rep.test(this.data.email)){
            my.alert({title: '请输入正确的邮箱格式！'});
            return false;
        }
        if(this.data.pwd.length<6){
            my.alert({title: '请输入六位或六位以上的密码'});
            return false;
        }
        if(this.data.pwd!==this.data.pwdComfirm){
           my.alert({title: '密码不一致'});
           return false
        }
        // console.log(this.data.email);
        // console.log(this.data.code);
        // console.log(this.data.pwd);
        // console.log(this.data.pwdComfirm);
        //调重置接口
        my.request({
            url: this.data.api+'/reset',
            method: 'POST',
            data: {
                email: this.data.email,
                password: this.data.pwd,
                code: this.data.code
            },
            headers:{
                'content-type':'application/json'  //默认值
            },
            dataType: 'json',
            success: function(res) {
                console.log(res)
                if(res.data.errCode==9){
                    my.showToast({
                        type: 'fail',
                        content: res.data.message,
                        duration: 2000
                    });
                }else if(res.data.errCode==0){
                    my.removeStorage({
                        key: 'userInfo'
                    });
                    my.removeStorage({
                        key: 'orderInfo'
                    });
                    my.removeStorage({
                        key: 'selectedAddress'
                    });
                    my.removeStorage({
                        key: 'shareInfo'
                    });
                    my.showToast({
                        type: 'success',
                        content: res.data.message,
                        duration: 2000
                    });
                    setTimeout(function(){
                        my.reLaunch({ url: '../login/login' });
                    },2000)
                }
            }
        });
    }
})